/**
 * @FileName: ServiceBean.java
 * @Package: com.ziroom.sms.std
 * @author liusq23
 * @created 2017/6/14 上午9:52
 * <p>
 * Copyright 2015 ziroom
 */
package com.ziroom.sms.std.spring;

import com.ziroom.sms.std.rpc.Exporter;
import com.ziroom.sms.std.rpc.Invoker;
import com.ziroom.sms.std.rpc.Protocol;
import com.ziroom.sms.std.rpc.ProxyFactory;
import com.ziroom.sms.std.rpc.protocol.SmsdboProtocol;
import com.ziroom.sms.std.rpc.proxyfacotry.JDKProxyFactory;
import com.ziroom.sms.std.spring.MethodConfig;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author liusq23
 * @since 1.0
 * @version 1.0
 */
public class ServiceBean<T> implements InitializingBean {

    private String id;
    //bean名称
    private String beanName;
    //引用类
    private T ref;
    //接口名称
    private String interfaceName;
    //接口时间
    private Class<T> interfaceClass;
    //方法配置
    private List<MethodConfig> methods;
    //协议
    private String protocol;

    public static final Protocol smsdboProtocol =  new SmsdboProtocol("127.0.0.1",20880);

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public T getRef() {
        return ref;
    }

    public void setRef(T ref) {
        this.ref = ref;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public List<MethodConfig> getMethods() {
        return methods;
    }

    public void setMethods(List<MethodConfig> methods) {
        this.methods = methods;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 开始暴露服务
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //获取调用
        ProxyFactory proxyFactory = new JDKProxyFactory();
        Invoker<T> invoker = proxyFactory.getInvoker(ref,interfaceClass);
        //暴露服务
        smsdboProtocol.exporter(invoker);
    }
}
